package com.ls.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ls.domain.Admin;
import com.ls.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    //登录
    @PostMapping("/login")
    public Admin login(HttpServletRequest request, @RequestBody Admin admin){
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getAdminName, admin.getAdminName())
                .eq(Admin::getAdminPassword,admin.getAdminPassword());
        Admin adm = adminService.getOne(queryWrapper);
        request.getSession().setAttribute("admin",adm.getId());
        return adm;
    }

    //登出
    @PostMapping("/logout")
    public boolean logout(HttpServletRequest request){
        request.getSession().removeAttribute("admin");
        return true;
    }

    //管理员改密码
    @PutMapping("/password/{id}")
    public boolean password(@RequestBody Admin admin){
        return adminService.updateById(admin);
    }
}
